package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.anno.ValidationAnno;
import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.User;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import cn.wolfcode.shop.cloud.util.GoodCodeMessage;
import cn.wolfode.shop.cloud.domain.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderInfoController {

    @Autowired
    private IOrderInfoService orderInfoService;

    @RequestMapping("/orderInfo")
    public Result orderInfo(String orderNo, @ValidationAnno User user){
        //判断用户是否登录
        if(user==null){
            throw new BusinessException(GoodCodeMessage.ORDERINFO_LOGIN_ERROR);
        }
        //判断以订单编号查询出来的订单详情对象是否为空
        OrderInfo orderInfo = orderInfoService.queryOrderInfo(orderNo);
        if(orderInfo==null){
            throw new BusinessException(GoodCodeMessage.ORDERINFO_MISTAKE_ERROR);
        }
        //判断查询到的订单数据是否是当前登录用户的订单数据
        if(!(orderInfo.getUserId().equals(user.getId()))){
            throw new BusinessException(GoodCodeMessage.ORDERINFO_ISEQL_ERROR);
        }
        return Result.success(orderInfo);
    }
}
